.PHONY: all

include ${P_BASE}/global/utils/make/target
include ${P_BASE}/global/utils/make/cmds
include ${P_BASE}/global/utils/make/escape

T_CC_FW_FLAGS   ?= ${T_CC_GLOBAL_FLAGS} -gstabs ${T_CC_FLAG_INC}${P_BASE} -nostdlib -nostdinc -fno-builtin
T_LIBGCC		?= $(shell $(call T_CC,,"",-print-libgcc-file-name))

# TRICK HERE: KEEP ORDER OF LINK
SRCFILES:= init.S $(filter-out init.S,$(shell $(call C_FIND,.,-iname "*.c" -or -iname "*.S") | $(call C_SED,-e 's!\./!!g')))
OBJFILES:= $(addprefix ${T_BASE}/fw_,\
		     $(addsuffix .o,$(foreach FILE,${SRCFILES},$(call E_ENCODE,${FILE}))))
DEPFILES:= $(OBJFILES:.o=.d)

all: ${T_BASE}/framework ${T_BASE}/framework.bin ${T_BASE}/framework.sym ${T_BASE}/framework.asm

-include $(DEPFILES)

${T_BASE}/fw_%.d:
	@$(call C_ECHO,DEP $(call E_DECODE,$*))
	${V}$(call T_CC, $(call E_DECODE,$*),$@,-MM $(T_CC_FW_FLAGS) -MT $(@:.d=.o))

${T_BASE}/fw_%.o: ${T_BASE}/fw_%.d
	@$(call C_ECHO,CC $(call E_DECODE,$*))
	${V}$(call T_CC,$(call E_DECODE,$*),$@,${T_CC_FW_FLAGS} ${T_CC_FLAG_OBJ})

${T_BASE}/modules_bfs.o: ${T_BASE}/modules
	${V}cd ${T_BASE}; $(call T_OBJCOPY,modules,modules_bfs.o,-I binary -O elf32-i386 -B i386)

${T_BASE}/framework: ${OBJFILES} ${T_BASE}/modules_bfs.o ${T_BASE}/libcommon_*.o
	@$(call C_ECHO,LD $@)
	${V}$(call T_LD,$^ ${T_LIBGCC},$@,-N -T framework.ld)

${T_BASE}/framework.bin: ${T_BASE}/framework
	@$(call C_ECHO,OBJCOPY $@)
	${V}$(call T_OBJCOPY,$^,$@,-S -Obinary)

${T_BASE}/framework.sym: ${T_BASE}/framework
	${V}$(call T_NM,$<,${T_BASE}/framework.nm,)
	${V}grep -i " t " ${T_BASE}/framework.nm | sed "s/ [tT] / /g" > $@
	${V}$(call C_RM,${T_BASE}/framework.nm)

${T_BASE}/framework.asm: ${T_BASE}/framework
	${V}$(call T_OBJDUMP,$<,$@,-S)
